import time
from utils import get_client


def run_batch_job(jsonl_file_path):
    """运行批量任务并返回任务ID"""
    client = get_client("SILICONFLOW")

    # 上传文件
    with open(jsonl_file_path, "rb") as f:
        batch_input_file = client.files.create(file=f, purpose="batch")

    if not hasattr(batch_input_file, 'id'):
        raise Exception(f"文件上传失败: {batch_input_file}")

    # 获取文件上传后的id
    file_id = batch_input_file.data['id']

    # 创建批量任务
    batches_job = client.batches.create(
        input_file_id=file_id,
        endpoint="/v1/chat/completions",
        completion_window="24h",
        metadata={"description": "招股书校对批量任务"},
    )

    if not hasattr(batches_job, 'id'):
        raise Exception(f"任务创建失败: {batches_job}")

    print(f"批量任务已创建，ID: {batches_job.id}")
    return batches_job.id